package com.zlkj.pro.modular.app.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zlkj.pro.core.exception.BusinessException;
import com.zlkj.pro.core.util.ConvertUtil;
import com.zlkj.pro.modular.app.mapper.AppUserMapper;
import com.zlkj.pro.modular.app.mapper.AppCustomerContactsMapper;
import com.zlkj.pro.modular.app.service.AppCustomerContactsService;
import com.zlkj.pro.modular.business.entity.CustomerContactsEntity;
import com.zlkj.pro.modular.business.model.dto.CustomerContactsDTO;
import com.zlkj.pro.modular.business.model.vo.AgentVO;
import com.zlkj.pro.modular.business.model.vo.CustomerContactsVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import static com.zlkj.pro.core.exception.enums.BusinessExceptionEnum.BUSINESS_EXCEPTION;

/**
 * @author liyang
 * @date 2024/4/6
 * @time 14:34
 */
@Slf4j
@Service
@RequiredArgsConstructor
@Transactional(rollbackFor = Exception.class)
public class AppCustomerContactsServiceImpl extends ServiceImpl<AppCustomerContactsMapper, CustomerContactsEntity> implements AppCustomerContactsService {

    @Autowired
    private AppCustomerContactsMapper appCustomerContactsMapper;

    @Autowired
    private AppUserMapper appUserMapper;


    @Override
    public List<CustomerContactsVO> findList(CustomerContactsDTO dto) {
        if (dto.getUserId() == null) throw new BusinessException(BUSINESS_EXCEPTION, "userId不能为空");

        // 查询个人代理商信息
        AgentVO vo = appUserMapper.getAgentInfo(dto.getUserId());
        dto.setAgentId(vo.getId());

        List<CustomerContactsVO> list = appCustomerContactsMapper.findList(dto);

        // 分页处理
        if (dto.getPageNum() != null && dto.getPageSize() != null) {
            list = list.stream().skip((dto.getPageNum() - 1) * dto.getPageSize()).limit(dto.getPageSize()).collect(Collectors.toList());
        }
        // 截取前20条记录
        /*if (CollectionUtils.isNotEmpty(list) && list.size() > 20) {
            list = list.stream().limit(20).collect(Collectors.toList());
        }*/
        return list;
    }

    @Override
    public boolean delete(CustomerContactsDTO dto) {
        if (dto.getId() == null) throw new BusinessException(BUSINESS_EXCEPTION, "id不能为空");
        return super.removeById(dto);
    }

    @Override
    public CustomerContactsVO findDetail(CustomerContactsDTO dto) {
        return appCustomerContactsMapper.findDetail(dto);
    }


    @Override
    public boolean add(CustomerContactsDTO dto) {

        // 联系人姓名、联系人电话，前后去空格
        if (StringUtils.isNotBlank(dto.getName())) dto.setName(dto.getName().trim());
        if (StringUtils.isNotBlank(dto.getPhone())) dto.setPhone(dto.getPhone().trim());

        AgentVO vo = appUserMapper.getAgentInfo(dto.getUserId());
        dto.setAgentId(vo.getId());
        dto.setCreateUser(dto.getUserId());// 当前登录人作为创建人
        dto.setUpdateTime(new Date());
        CustomerContactsEntity entity = ConvertUtil.turn(dto, CustomerContactsEntity.class);
        return super.save(entity);
    }

    @Override
    public boolean edit(CustomerContactsDTO dto) {

        // 联系人姓名、联系人电话，前后去空格
        if (StringUtils.isNotBlank(dto.getName())) dto.setName(dto.getName().trim());
        if (StringUtils.isNotBlank(dto.getPhone())) dto.setPhone(dto.getPhone().trim());

        dto.setUpdateUser(dto.getUserId());// 当前登录人作为更新
        CustomerContactsEntity entity = ConvertUtil.turn(dto, CustomerContactsEntity.class);
        return super.updateById(entity);
    }


}
